package zy.blue7.gui.netty.custom.qq;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.handler.timeout.IdleStateHandler;
import zy.blue7.gui.netty.custom.qq.codec.MessageCodec;
import zy.blue7.gui.netty.custom.qq.codec.MessageInfoCodec;
import zy.blue7.gui.netty.custom.qq.message.*;
import zy.blue7.gui.netty.custom.qq.utils.SerializableUtils;

import java.net.InetSocketAddress;
import java.util.concurrent.TimeUnit;

/**
 * @Author: blue7
 * @Date: 2022/2/27 18:35
 * @Version 1.0
 * @Description
 */
public class Client {
    public static void main(String[] args) throws InterruptedException {
        new Bootstrap()
                // 2.添加 EventLoop
                .group(new NioEventLoopGroup())
                //3. 选择客户端 channel实现
                .channel(NioSocketChannel.class)
                //4.添加处理器
                .handler(new ChannelInitializer<NioSocketChannel>() {
                    @Override // todo 在建立链接的时候调用
                    protected void initChannel(NioSocketChannel ch) throws Exception {

                        ch.pipeline().addLast(new IdleStateHandler(6,5,7, TimeUnit.SECONDS));
                        ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
                            @Override
                            public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
                                if (evt instanceof IdleStateEvent) {

                                    IdleStateEvent event = (IdleStateEvent) evt;
                                    /**
                                     *
                                     */
                                    IdleState state = event.state();

                                    switch (state) {
                                        case ALL_IDLE:
                                            System.out.println("读写空闲");
                                            break;
                                        case READER_IDLE:
                                            System.out.println("读空闲");
                                            EmptyMessage message1 = new EmptyMessage();
                                            ctx.channel().writeAndFlush(message1);
                                            System.out.println("读空闲...");
                                            break;
                                        case WRITER_IDLE:
                                            System.out.println("写空闲");
                                            break;
                                    }

                                    /**
                                     * 服务器进行响应的处理
                                     */
                                }
                            }
                        });
                        ch.pipeline().addLast(new MessageInfoCodec());
                        ch.pipeline().addLast(new MessageCodec());
//                        ch.pipeline().addLast(new MessageInfoDecoder());
//                        ch.pipeline().addLast(new MessageInfoEncoder());
                        ch.pipeline().addLast(new SimpleChannelInboundHandler<Message>() {
                            @Override
                            public void channelActive(ChannelHandlerContext ctx) throws Exception {

                                /**
                                 * 下面是发送 登录消息
                                 */
                                LoginMessage message1 = new LoginMessage();
                                message1.setFrom("张三");
                                message1.setContent("你好 ，李四");
                                message1.setTo("李四");
                                message1.setUsername("00");
                                message1.setPassword("-----");


                                ctx.writeAndFlush(message1);

//                                /**
//                                 *
//                                 */
//                                LoginMessage message2 = new LoginMessage();
//                                message2.setFrom("李四");
//                                message2.setContent("你好 ，张三");
//                                message2.setTo("张三");
//                                message2.setUsername("00");
//                                message2.setPassword("-----");
//
//
//                                ctx.writeAndFlush(message2);


//                                for (int i = 0; i < 6; i++) {
//
//                                    SingleMessage message = new SingleMessage();
//                                     message.setFrom("张三");
//                                     message.setType(1);
//                                     message.setContent("你好 ，李四");
//                                     message.setTo("李四");
//
//
//                                    ctx.writeAndFlush(message);
//                                }
                                for (int i = 0; i < 6; i++) {

                                    GroupMessage message = new GroupMessage();
                                     message.setFrom("张三");
                                     message.setType(1);
                                     message.setContent("你好 ，李四");
                                     message.setTo("李四");

                                    ctx.writeAndFlush(message);
                                }

                            }

                            @Override
                            protected void channelRead0(ChannelHandlerContext channelHandlerContext, Message message) throws Exception {
                                System.out.println(message);
                                System.out.println(message.getClass());
                                System.out.println("-=========================");
                            }
                            @Override
                            public void channelInactive(ChannelHandlerContext ctx) throws Exception {
                                ctx.channel().close();
                            }
                        });

                    }
                })
                //5.连接到服务器
                .connect(new InetSocketAddress("localhost", 8080))
                .sync() //todo 阻塞方法，知道链接建立
                .channel();
//                .writeAndFlush("nihao");


    }
}
